package com.jnzc.ocr.serv.shop.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jnzc.ocr.common.error.BusinessException;
import com.jnzc.ocr.common.utils.CalculateUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.dependencies.serv.mybatis.MybatisServiceImpl;
import com.jnzc.ocr.serv.shop.entity.ShopProductStock;
import com.jnzc.ocr.serv.shop.filter.ShopProductStockFilter;
import com.jnzc.ocr.serv.shop.mapper.ShopProductStockMapper;
import com.jnzc.ocr.serv.shop.service.ShopProductStockService;
import com.jnzc.ocr.serv.shop.vo.ShopProductStockChangeVo;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author zhangcm
 * @version 1.0, 2017-10-06
 */
@Service
public class ShopProductStockServiceImpl extends MybatisServiceImpl<ShopProductStockMapper, ShopProductStock, ShopProductStockFilter> implements ShopProductStockService {

    @Override
    protected void fillEntityWrapper(EntityWrapper<ShopProductStock> ew, ShopProductStockFilter filter) {
        ShopProductStock entity = new ShopProductStock();
        entity.setProductId(filter.getProductId());
        entity.setSkuId(filter.getSkuId());
        ew.setEntity(entity);
        if(null != filter.getHasSkuId()){
            if(filter.getHasSkuId()){
                ew.isNotNull("sku_id");
            }else{
                ew.isNull("sku_id");
            }
        }
    }

    @Override
    public ShopProductStock find(Long productId, Long skuId, Long shopId) {
        ValidateUtils.isId(productId);
        ShopProductStockFilter filter = new ShopProductStockFilter();
        filter.setProductId(productId);
        if(null == skuId) {
            filter.setHasSkuId(false);
        } else {
            filter.setSkuId(skuId);
        }
        ShopProductStock obj = super.find(filter);
        if(null == obj){
            obj = new ShopProductStock();
            obj.setShopId(shopId);
            obj.setProductId(productId);
            obj.setSkuId(skuId);
        }
        obj.setStockNum(CalculateUtils.def(obj.getStockNum()));
        obj.setSaleNum(CalculateUtils.def(obj.getSaleNum()));
        return obj;
    }

    @Override
    public void updateNum(List<ShopProductStockChangeVo> list, Long shopId) {
        Map<String, ShopProductStock> stockMap = Maps.newHashMap();
        for (ShopProductStockChangeVo vo : list) {
            Integer addStockNum = vo.getAddStockNum();
            Integer addSaleNum = vo.getAddSaleNum();
            if(null != vo.getSkuId()) {
                ShopProductStock skuStock = this.findFromMap(vo.getProductId(), vo.getSkuId(), shopId, stockMap);
                ShopProductStock productStock = this.findFromMap(vo.getProductId(), null, shopId, stockMap);
                addStockNum = getAddNum(skuStock.getStockNum(), vo.getAddStockNum(), vo.getNewStockNum());
                addSaleNum = getAddNum(skuStock.getSaleNum(), vo.getAddSaleNum(), vo.getNewSaleNum());
                // 修改sku库存、销量
                skuStock.setStockNum(CalculateUtils.add(skuStock.getStockNum(), addStockNum));
                skuStock.setSaleNum(CalculateUtils.add(skuStock.getSaleNum(), addSaleNum));
                // 修改产品库存、销量
                productStock.setStockNum(CalculateUtils.add(productStock.getStockNum(), addStockNum));
                productStock.setSaleNum(CalculateUtils.add(productStock.getSaleNum(), addSaleNum));
            } else {
                ShopProductStock productStock = this.findFromMap(vo.getProductId(), null, shopId, stockMap);
                addStockNum = getAddNum(productStock.getStockNum(), vo.getAddStockNum(), vo.getNewStockNum());
                addSaleNum = getAddNum(productStock.getSaleNum(), vo.getAddSaleNum(), vo.getNewSaleNum());
                // 修改产品库存、销量
                productStock.setStockNum(CalculateUtils.add(productStock.getStockNum(), addStockNum));
                productStock.setSaleNum(CalculateUtils.add(productStock.getSaleNum(), addSaleNum));
            }
        }
        this.saveList(Lists.newArrayList(stockMap.values()));
    }

    private ShopProductStock findFromMap(Long productId, Long skuId, Long shopId, Map<String, ShopProductStock> stockMap) {
        String key = null != skuId ? productId + "-" + skuId : productId + "";
        ShopProductStock stock = stockMap.get(key);
        if(null == stock) {
            stock = this.find(productId, skuId, shopId);
            stockMap.put(key, stock);
        }
        return stock;
    }

    private Integer getAddNum(Integer oldNum, Integer addNum, Integer newNum) {
        if(null != addNum && null != newNum) {
            throw new BusinessException("addNum和newNum不能共存");
        } else if(null == addNum && null == newNum) {
            return 0;
        }
        return null != addNum ? addNum : CalculateUtils.sub(newNum, oldNum);
    }
}
